package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwzxy.medic.entity.Familyhold;
import com.gxwzxy.medic.service.FamilyholdService;
import com.gxwzxy.medic.service.Impl.FamilyholdServiceImpl;
import com.gxwzxy.medic.until.PageBean;


/**
 * Servlet implementation class FamilyholeServlet
 */
public class FamilyholdServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
     
	//饿汉式的不安全的声明一个状态
	FamilyholdService fhs = new FamilyholdServiceImpl();
	
	/**
	 * 分页查询所有参合农民
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void allFamilyhold(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String sindex = request.getParameter("index");
    	int index = 1;
    	try {
			index = Integer.parseInt(sindex);
		} catch (NumberFormatException n) {
			n.printStackTrace();
		}
    	
    	PageBean<Familyhold> pfh = new PageBean<Familyhold>();
    	pfh.setIndex(index);
    	fhs.pageFamilyhold(pfh);
    	
    	request.setAttribute("pfh", pfh);
    	request.getRequestDispatcher("page/family/familyholdAll_list.jsp").forward(request, response);
	}
	
	/**
	 * 查询家庭参合人员列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findAllFamilyhold(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String jtbh = request.getParameter("jtbh");
		List<Familyhold> lfh = fhs.findAllFamilyhold(jtbh);
		request.setAttribute("listFamilyhold", lfh);
		request.setAttribute("jtbh", jtbh);
		request.getRequestDispatcher("page/family/familyhold_list.jsp").forward(request, response);
	}
	/**
	 * 根据id查参合人员信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findByIdFamilyhold(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		
		Familyhold familyhold = fhs.findById(id);
		
		request.setAttribute("Familyhold", familyhold);
		request.getRequestDispatcher("page/family/familyhold_edit.jsp").forward(request, response);
	}
	
	/**
	 * 添加参合人员信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addFamilyhode(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
	
		//获取请求数据
		String jtbh = request.getParameter("jtbh");
		String nhzh = request.getParameter("nhzh");
		String ylzkh = request.getParameter("ylzkh");
		String hnbh = request.getParameter("hnbh");
		String xm = request.getParameter("xm");
		String yhzgx = request.getParameter("yhzgx");
		String sfzh = request.getParameter("sfzh");
		String xb = request.getParameter("xb");
		String jkzk = request.getParameter("jkzk");
		String mz = request.getParameter("mz");
		String whcd = request.getParameter("whcd");
		String nl = request.getParameter("nl");
		String csrq = request.getParameter("csrq");
		String rysx = request.getParameter("rysx");
		String sfnchk = request.getParameter("sfnchk");
		String zy = request.getParameter("zy");
		String gzdw = request.getParameter("gzdw");
		String lxdh = request.getParameter("lxdh");
		String czdz = request.getParameter("czdz");
		
		
		Familyhold familyhold = new Familyhold();
		familyhold.setJtbh(jtbh);
		familyhold.setNhzh(nhzh);
		familyhold.setYlzkh(ylzkh);
		familyhold.setHnbh(hnbh);
		familyhold.setXm(xm);
		familyhold.setYhzgx(yhzgx);
		familyhold.setSfzh(sfzh);
		familyhold.setXb(xb);
		familyhold.setJkzk(jkzk);
		familyhold.setMz(mz);
		familyhold.setWhcd(whcd);
		familyhold.setNl(nl);
		familyhold.setCsrq(csrq);
		familyhold.setRysx(rysx);
		familyhold.setSfnchk(sfnchk);
		familyhold.setZy(zy);
		familyhold.setGzdw(gzdw);
		familyhold.setLxdh(lxdh);
		familyhold.setCzdz(czdz);
		
		int i = fhs.addMamilyhold(familyhold);
		if(i>0) {
			request.setAttribute("url", "FamilyholdServlet?method=findAllFamilyhold&jtbh="+jtbh);
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	/**
	 * 修改参合农民信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateFamilyhode(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		//获取请求数据
		//强转
		int id = Integer.parseInt(request.getParameter("id"));
		String jtbh = request.getParameter("jtbh");
		String nhzh = request.getParameter("nhzh");
		String ylzkh = request.getParameter("ylzkh");
		String hnbh = request.getParameter("hnbh");
		String xm = request.getParameter("xm");
		String yhzgx = request.getParameter("yhzgx");
		String sfzh = request.getParameter("sfzh");
		String xb = request.getParameter("xb");
		String jkzk = request.getParameter("jkzk");
		String mz = request.getParameter("mz");
		String whcd = request.getParameter("whcd");
		String nl = request.getParameter("nl");
		String csrq = request.getParameter("csrq");
		String rysx = request.getParameter("rysx");
		String sfnchk = request.getParameter("sfnchk");
		String zy = request.getParameter("zy");
		String gzdw = request.getParameter("gzdw");
		String lxdh = request.getParameter("lxdh");
		String czdz = request.getParameter("czdz");
		
		Familyhold familyhold = new Familyhold();
		familyhold.setId(id);
		familyhold.setJtbh(jtbh);
		familyhold.setNhzh(nhzh);
		familyhold.setYlzkh(ylzkh);
		familyhold.setHnbh(hnbh);
		familyhold.setXm(xm);
		familyhold.setYhzgx(yhzgx);
		familyhold.setSfzh(sfzh);
		familyhold.setXb(xb);
		familyhold.setJkzk(jkzk);
		familyhold.setMz(mz);
		familyhold.setWhcd(whcd);
		familyhold.setNl(nl);
		familyhold.setCsrq(csrq);
		familyhold.setRysx(rysx);
		familyhold.setSfnchk(sfnchk);
		familyhold.setZy(zy);
		familyhold.setGzdw(gzdw);
		familyhold.setLxdh(lxdh);
		familyhold.setCzdz(czdz);
		
		
		int i = fhs.update(familyhold);
		if(i>0) {
			request.setAttribute("url", "FamilyholdServlet?method=findAllFamilyhold&jtbh="+jtbh);
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	
	/**
	 * 删除参合农民信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteFamilyhold(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		
		int i = fhs.delete(id);
		if(i>0) {
			response.getWriter().write("true");
		}
	}
}
